<?
        require_once '../Navigation/functions.php';
        require_once 'data.php';
        $section='Users';
        $IsAjax = isset($_REQUEST['IsAjax']);
        if(isset($_REQUEST['Save']))
        {
                $rs = $_REQUEST;
                $errors = Users::Validate($rs);
                if(!$errors)
                        $errors = Users::Save($rs);
                if(!$errors)
                {
                        if($IsAjax)
                        {
                                ob_start();
                                include 'index_details.php';
                                $html = ob_get_clean();
                                echo json_encode(array( "message" => "Success", "html"=> $html));
                                die();
                        }
                        else
                                header("Location: index.php?new_id=$rs[id]");
                }
        }
        elseif(isset($_REQUEST['id']))
                $rs = Users::Get($_REQUEST['id']);
        else
                $rs = Users::Blank();
                
?>

<? if($IsAjax){
                ob_start();     
?>
        <form method="post" autocomplete="on" action="edit.php?IsAjax=true<?=isset($_REQUEST['id']) ? "&id=$_REQUEST[id]" : ''?>">
                <input  type="text"
                        name="FirstName" id="FirstName"
                    value="<?=$rs['FirstName']?>"
                    class="<?=isset($errors['FirstName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FirstName']?></span>
                <input  type="text"
                        name="LastName" id="LastName"
                    value="<?=$rs['LastName']?>"
                    class="<?=isset($errors['LastName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['LastName']?></span>
                <input  type="text"
                        name="PictureURL" id="PictureURL"
                    value="<?=$rs['PictureURL']?>"
                    class="<?=isset($errors['PictureURL']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['PictureURL']?></span>
                <input  type="text"
                        name="FBID" id="FBID"
                    value="<?=$rs['FBID']?>"
                    class="<?=isset($errors['FBID']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FBID']?></span>
                <input  type="number"
                        name="Lookup_id" id="Lookup_id"
                    value="<?=$rs['Lookup_id']?>"
                    class="<?=isset($errors['Lookup_id']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['Lookup_id']?></span>
            <input type="hidden" value="true"  name="Save"/>
            <input type="submit" value="Save"/>
            <a href="#" class="cancel">Cancel</a>
       </form>

<?
                if(!$errors) $message = "Success";
                else $message = "Error";
                $html = ob_get_clean();
                echo json_encode(array( "message" => $message, "html" => $html, "errors"=> implode(",",$errors) ));
}else{ ?>
<!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Users</title>
        <style type="text/css">
                        .error { color: red; }
                        input.error {
                                border: 1px solid red;
                                background: #FEE;
                        }
                        form {
                                width: 300px;
                                float: left;
                        }
                </style>
    </head>
    
    <body>
                <? include("../Navigation/tabs.php"); ?>
        
        <h1>Edit a Contact Method</h1>
        <ul class="error">
                <? foreach($errors as $field => $error){ ?>
                <li><?=$field?>: <?=$error?></li>
            <? } ?>
        </ul>
        
        <form method="post" autocomplete="on" action="">
                <label>id:</label>
                <?=$rs['id']?>
            <br />
                <label for="FirstName">First Name</label>
                <input  type="text"
                        name="FirstName" id="FirstName"
                    value="<?=$rs['FirstName']?>"
                    class="<?=isset($errors['FirstName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FirstName']?></span>
            <br />
                <label for="LastName">Last Name</label>
                <input  type="text"
                        name="LastName" id="LastName"
                    value="<?=$rs['LastName']?>"
                    class="<?=isset($errors['LastName']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['LastName']?></span>
            <br />
                <label for="Password">Password</label>
                <input  type="text"
                        name="Password" id="Password"
                    value="<?=$rs['Password']?>"
                    class="<?=isset($errors['Password']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['Password']?></span>
            <br />
                <label for="Lookup_id">Lookup_id</label>
                <input  type="number"
                        name="Lookup_id" id="Lookup_id"
                    value="<?=$rs['Lookup_id']?>"
                    class="<?=isset($errors['Lookup_id']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['Lookup_id']?></span>
            <br />
                <label for="PictureURL">PictureURL</label>
                <input  type="text"
                        name="PictureURL" id="PictureURL"
                    value="<?=$rs['PictureURL']?>"
                    class="<?=isset($errors['PictureURL']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['PictureURL']?></span>
            <br />
                <label for="FBID">FBID</label>
                <input  type="text"
                        name="FBID" id="FBID"
                    value="<?=$rs['FBID']?>"
                    class="<?=isset($errors['FBID']) ? 'error' : '' ?>" />
            <span class="error"><?=$errors['FBID']?></span>
            <br />
            <input type="submit" value="Save"  name="Save"/>
        </form>
        <div style="float:left">
        <?
                        $_REQUEST['User_id'] = $rs['id'];
                        include '../ContactMethods/index.php';
                ?>
        </div>
        </body>
</html>
<? } ?>

